#!/bin/bash
###
 # @Author: jiu yin
 # @Date: 2023-08-10 14:12:51
 # @LastEditTime: 2023-08-10 15:18:17
 # @LastEditors: jiu yin zhen jing
 # @FilePath: \AI admin\admin.sh
 # jiu
###
name=ai_admin

# shellcheck disable=SC1083
VERSION=$(docker image ls --format {{.Tag}} $name)

# 检查版本号是否为空
if [ -z "$VERSION" ]; then
  VERSION="1.0.1"
fi

IFS='.' read -ra VERSION_PARTS <<< "$VERSION"

# shellcheck disable=SC2004
VERSION_PARTS[2]=$((${VERSION_PARTS[2]} + 1))


if [ ${VERSION_PARTS[2]} -eq 10 ]; then
  # shellcheck disable=SC2004
  VERSION_PARTS[1]=$((${VERSION_PARTS[1]} + 1))
  VERSION_PARTS[2]=0
fi


if [ ${VERSION_PARTS[1]} -eq 10 ]; then
  # shellcheck disable=SC2004
  VERSION_PARTS[0]=$((${VERSION_PARTS[0]} + 1))
  VERSION_PARTS[1]=0
fi

# 将新版本号输出为字符串
NEW_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.${VERSION_PARTS[2]}"


docker rmi $name:"$VERSION"


docker rmi registry.cn-chengdu.aliyuncs.com/tanmeta/$name:"$VERSION"

docker build -t $name:$NEW_VERSION   .


IMAGE_ID=$(docker images -q $name)


docker tag "$IMAGE_ID" registry.cn-chengdu.aliyuncs.com/tanmeta/$name:$NEW_VERSION


docker push registry.cn-chengdu.aliyuncs.com/tanmeta/$name:$NEW_VERSION